package com.newcoder.controller;

import com.newcoder.entity.User;
import com.newcoder.service.impl.UserServiceImpl;
import com.newcoder.util.ResponseWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/Login")
public class LoginController {

    // private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserServiceImpl userService;

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rememberme",value = "rememberme"),
            @ApiImplicitParam(name = "user",value = "登录表单"),
    })
    @PostMapping("/login")
    public ResponseWrapper login(@RequestParam(name = "rememberme")boolean rememberme, @RequestBody User user){
        ResponseWrapper wrapper = userService.login(rememberme, user);
        return wrapper;
    }

    @ApiOperation("生成登录的验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "response",value = "浏览器参数，调用接口无需传参"),
    })
    @GetMapping("/kaptcha")
    /**
     * 在SpringBoot3中HttpServletResponse，需要导入import jakarta.servlet.http.HttpServletResponse;
     *                                    不能导入import javax.servlet.http.HttpServletResponse;，
     * 否则会出现500错误
     * */
    public void kaptcha(HttpServletResponse response){
        userService.kaptcha(response);
    }

    @ApiOperation("退出登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "登录凭证"),
    })
    @GetMapping("/logout")
    public ResponseWrapper logout(@RequestParam(name = "token")String token){
        ResponseWrapper wrapper = userService.logout(token);
        return wrapper;
    }

    @ApiOperation("注册账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "注册表单"),
    })
    @PostMapping("/register")
    public ResponseWrapper register(@RequestBody User user){
        ResponseWrapper wrapper = userService.register(user);
        return wrapper;
    }

    @ApiOperation("激活账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id"),
            @ApiImplicitParam(name = "activationCode",value = "激活码"),
    })
    @GetMapping("/activation")
    public ResponseWrapper activation(@RequestParam(name = "username")String username, @RequestParam(name = "activationCode")String activationCode){
        ResponseWrapper wrapper = userService.activation(username, activationCode);
        return wrapper;
    }
}
